package com.wsd.action.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;

import com.wsd.action.BaseAction;
import com.wsd.bean.Category;
import com.wsd.dao.impl.CategoryDAO;
import com.wsd.service.CategoryService;
import com.wsd.util.TreeUtil;

public class CategoryAction extends BaseAction {

	private CategoryService categoryService;
	private CategoryDAO	categoryDAO;
	private Category parent;
	private Category category;
	private String[] cNames;
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}
	

	public void setCategoryDAO(CategoryDAO categoryDAO) {
		this.categoryDAO = categoryDAO;
	}

	@JSON(serialize = false)
	public Category getParent() {
		return parent;
	}

	@JSON(serialize = false)
	public String[] getCNames() {
		return cNames;
	}


	public void setParent(Category parent) {
		this.parent = parent;
	}


	public void setCNames(String[] cNames) {
		this.cNames = cNames;
	}
	
	

	@JSON(serialize = false)
	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}
	

	public String categoryTreeData() throws IOException{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8");
		Category root = categoryService.getRootCategorie();
		String treeDate = TreeUtil.createTreeJSON(root, false);
		System.out.println(treeDate);
		response.getWriter().write(treeDate);
		response.getWriter().close();
		return null;
	}
	
	public String addCategories(){
		//Category[] categories = new Category[cNames.length];
		List<Category> categories = new ArrayList<Category>();
		int level = parent.getCLevel() + 1;
		int len = cNames.length;
		Category nParent = categoryService.findById(parent.getCId());
		int rgt = nParent.getRgt();
		for(int i = 1; i <= len; i++){
			Category category = new Category();
			category = new Category();
			category.setCName(cNames[i - 1]);
			category.setCLevel(level);
			category.setParent(parent);
			categories.add(category);
			category.setLft(rgt++);
			category.setRgt(rgt++);
		}
		boolean flag = true;
		try{
			categoryService.saveCategories(categories);
			result.put("categories", categories);
			result.put("addSize", categories.size());
		}catch (Exception e) {
			flag = false;
		}
		result.put("addResult", flag);
		return AJAX;
	}
	
	/**
	 * 删除类别
	 * @return
	 */
	public String delCategorie(){
		
		categoryService.delCategory(category);
		result.put("msg", "删除成功");
		return AJAX;
	}
	
	/**
	 * 修改类别名称
	 */
	public String modifyCategorie(){
		String newName = category.getCName();
		category = categoryDAO.findById(category.getCId());
		category.setCName(newName);
		categoryService.update(category);
		result.put("modifyResult", true);
		return AJAX;
	}
	
}
